﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;

using System.Windows.Forms;
using System.Drawing;

namespace TrainSimulatorGUI.CustomControls
{
    /// <summary>
    /// Modified GroupBox supporting BorderColor
    /// </summary>
    public partial class GroupBoxMod : GroupBox
    {
        public Color BorderColor { get; set; }

        public GroupBoxMod()
        {
            InitializeComponent();
        }

        public GroupBoxMod(IContainer container)
            : this()
        {
            container.Add(this);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            e.Graphics.Clear(this.BackColor);
            var border = (Rectangle)e.ClipRectangle;
            Size tSize = TextRenderer.MeasureText(this.Text, this.Font);
            using (Pen p = new Pen(BorderColor, 1))
            {
                // Right of button/text
                e.Graphics.DrawLine(p, border.X + 7 + tSize.Width * 1.2f, 7, this.Width - 4, 7);
                e.Graphics.DrawLine(p, this.Width - 4, 8, this.Width - 3, 8);
                e.Graphics.DrawLine(p, this.Width - 3, 9, this.Width - 2, 9);
                e.Graphics.DrawLine(p, this.Width - 2, 10, this.Width - 2, this.Height - 3);
                // Left of button
                e.Graphics.DrawLine(p, border.X + 3, 7, border.X + 7, 7);
                e.Graphics.DrawLine(p, border.X + 2, 8, 3, 8);
                e.Graphics.DrawLine(p, border.X + 1, 9, 2, 9);
                e.Graphics.DrawLine(p, border.X + 1, 10, 1, this.Height - 3);
                // Bottom
                e.Graphics.DrawLine(p, border.X + 3, this.Height - 1, this.Width - 4, this.Height - 1);
                e.Graphics.DrawLine(p, border.X + 2, this.Height - 2, 3, this.Height - 2);
                e.Graphics.DrawLine(p, border.X + 1, this.Height - 3, 2, this.Height - 3);
                e.Graphics.DrawLine(p, this.Width - 3, this.Height - 2, this.Width - 4, this.Height - 2);
                e.Graphics.DrawLine(p, this.Width - 2, this.Height - 3, this.Width - 3, this.Height - 3);

                e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), new PointF(border.X + 11, 0));
            }
        }
    }
}
